#include "friend.h"
#include "protocol.h"
#include "tcpclient.h"
#include <QDebug>
#include <QTcpSocket>
#include <QInputDialog>
#include <QMessageBox>
#include "privatechat.h"

Friend::Friend(QWidget *parent)
    : QWidget{parent}
{
    m_showMsgTE = new QTextEdit;
    m_friendListWidget = new QListWidget;
    m_inputMsgLE = new QLineEdit;

    m_del_friendPB = new QPushButton("删除好友");
    m_flush_friendPB = new QPushButton("刷新好友");
    m_show_online_userPB = new QPushButton("显示在线用户");
    m_search_userPB = new QPushButton("查询用户");
    m_private_chatPB = new QPushButton("私聊");
    m_group_chatPB = new QPushButton("群聊");

    QVBoxLayout* right_vl = new QVBoxLayout;
    right_vl->addWidget(m_del_friendPB);
    right_vl->addWidget(m_flush_friendPB);
    right_vl->addWidget(m_show_online_userPB);
    right_vl->addWidget(m_search_userPB);
    right_vl->addWidget(m_private_chatPB);

    QHBoxLayout* h_vl = new QHBoxLayout;
    h_vl->addWidget(m_showMsgTE);
    h_vl->addWidget(m_friendListWidget);
    h_vl->addLayout(right_vl);

    QHBoxLayout* lower_vl = new QHBoxLayout;
    lower_vl->addWidget(m_inputMsgLE);
    lower_vl->addWidget(m_group_chatPB);

    m_online = new OlineUI;

    QVBoxLayout* all_vl = new QVBoxLayout;
    all_vl->addLayout(h_vl);
    all_vl->addLayout(lower_vl);
    all_vl->addWidget(m_online);
    m_online->hide();

    setLayout(all_vl);

    //当点击"显示在线用户"时, 显示或隐藏online界面
    connect(m_show_online_userPB, &QPushButton::clicked, this, [&]() {
        // emit online_show();
        if (m_online->isHidden()) {
            m_online->show();
            this->send_online_msg();
        } else
            m_online->hide();
    });

    //查询用户
    connect(m_search_userPB, &QPushButton::clicked, this, [&]() {
        search_name = QInputDialog::getText(this, "输入用户名", "用户名:");
        if (!search_name.isEmpty()) {

            PDU* pdu = makePDU(0); //注册的实际消息长度=0
            pdu->uiMsgType = ENUM_MSG_TYPE_SEARCH_REQUEST;
            strncpy(pdu->caData, search_name.toStdString().c_str(), 32);
            TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen); //socket 发送
            // pdu->printPDU();
            free(pdu);
            pdu = NULL;
        }
    });

    connect(m_flush_friendPB, &QPushButton::clicked, this, [&]() {
        this->flush_friend_func();  //写成独立的函数， 别处方便调用
    });

    connect(m_del_friendPB, &QPushButton::clicked, this, [&]() {
        QListWidgetItem * item = m_friendListWidget->currentItem();
        if (item) {
            QString user_name = item->text();
            QString my_name = TcpClient::getInstance().get_login_name();
            // qDebug() << user_name << " -" << my_name;

            PDU* pdu = makePDU(0); //注册的实际消息长度=0
            pdu->uiMsgType = ENUM_MSG_TYPE_DEL_FRIEND_REQUEST;
            strncpy(pdu->caData, user_name.toStdString().c_str(), user_name.size());
            strncpy(pdu->caData+32, my_name.toStdString().c_str(), my_name.size());

            TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen); //socket 发送
            // pdu->printPDU();
            free(pdu);
            pdu = NULL;
        } else {
            QMessageBox::critical(NULL, "没有选择用户", "请选择在线好友");
        }
    });

    connect(m_private_chatPB, &QPushButton::clicked, this, [&]() {
        QListWidgetItem * item = m_friendListWidget->currentItem();
        if (item) {
            // QString user_name = item->text();
            PrivateChat::getInstance().set_name(item->text());
            if (PrivateChat::getInstance().isHidden())
                PrivateChat::getInstance().show();
        } else {
            QMessageBox::critical(NULL, "没有选择用户", "请选择在线好友");
        }
    });

    connect(m_group_chatPB, &QPushButton::clicked, this, [&]() {
        QString msg = m_inputMsgLE->text();
        m_inputMsgLE->clear();
        if (!msg.isEmpty()) {
            PDU* pdu = makePDU(msg.size()+1); //注册的实际消息长度=0
            pdu->uiMsgType = ENUM_MSG_TYPE_GROUP_CHAT_REQUEST;
            QString log_name = TcpClient::getInstance().get_login_name();
            strncpy(pdu->caData, log_name.toStdString().c_str(), log_name.size());
            strncpy((char*)pdu->caMsg, msg.toStdString().c_str(), msg.size());
            TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen); //socket 发送
            // pdu->printPDU();
            free(pdu); pdu = NULL;
        } else {
            QMessageBox::critical(NULL, "发送消息为空", "发送消息不能为空");
        }
    });
}

void Friend::show_online_user(PDU *pdu)
{
    if (!pdu) return;
    m_online->show_user(pdu);
}

void Friend::show_online_friend(PDU *pdu)
{
    if (!pdu) return;
    m_friendListWidget->clear();
    QStringList names;
    uint msg_len = pdu->uiMsgLen;
    int size_ = msg_len / 32;
    char* ch;
    for (int i = 0; i < size_; i++) {
        ch = (char*)(pdu->caMsg)+i*32;
        qDebug() << ch;
        // ui->listWidget->addItem(QString(QLatin1String(ch)));
        m_friendListWidget->addItem(ch);
    }
}

void Friend::show_group_msg(PDU *pdu)
{
    pdu->printPDU();
    QString msg = QString("[%1]say: %2").arg(pdu->caData).arg( (char*)pdu->caMsg);
    this->m_showMsgTE->append(msg);
}

void Friend::flush_friend_func()
{
    PDU* pdu = makePDU(0); //注册的实际消息长度=0
    pdu->uiMsgType = ENUM_MSG_TYPE_FLUSH_FRIEND_REQUEST;
    QString log_name = TcpClient::getInstance().get_login_name();
    strncpy(pdu->caData, log_name.toStdString().c_str(), log_name.size());
    TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen); //socket 发送
    // pdu->printPDU();
    free(pdu); pdu = NULL;
}

QListWidget *Friend::friendListWidget() const
{
    return m_friendListWidget;
}

void Friend::send_online_msg()
{
    PDU* pdu = makePDU(0); //注册的实际消息长度=0
    pdu->uiMsgType = ENUM_MSG_TYPE_SHOW_ONLINE_REQUEST;
    TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen); //socket 发送
    // pdu->printPDU();
    free(pdu);
    pdu = NULL;
}




